﻿using AbstractSingleAixdll;
using AccustomeAttributedll;
using AixCommInfo;
using ISaveReaddll;
using SingleAixdll;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Reflection;
using System.Runtime.ConstrainedExecution;
using System.Text;
using System.Threading.Tasks;

namespace FinalTestMachine.Motion.Aixs
{
    public abstract class AixCreateBase : AbstractSingleAixdll.AbstractSingleAix, IProxyMov, ISaveReaddll.ISaveRead
    {
        public AixCreateBase(ApplicationSettingsBase applicationSettingsBase, string keyName = null) : base(CreateSingleZMotionAix(applicationSettingsBase), CreateAbstractAixCreateArgs(applicationSettingsBase), StaticAixInfoRegisterManager.aixRegisterInfo)
        {
            if (string.IsNullOrEmpty(keyName))
            {
                SaveRead = new SaveReaddll.SaveReadHeler(() => ParamPath_Motion.CurFileFullPath, () => this);
            }
            else
            {
                SaveRead = new SaveReaddll.SaveReadHeler(() => ParamPath_Motion.CurFileFullPath, () => this, section: keyName);
            }
        }

        #region 正运动
        class ISingleAixCfgForZMotion
        {
            public string IP { get; set; }

            public string Name { get; set; }

            public short DevNo { get; set; }

            public SingleAixdll.SvonLogic SvonLogic { get; set; }
        }

        private static ISingleAix CreateSingleZMotionAix(ApplicationSettingsBase applicationSettingsBase)
        {
            return CreateSingleAix<ISingleAixCfgForZMotion>(applicationSettingsBase, s =>
            {
                return new ZMotionHelper.SingleAix.SingleAixHelper
                (
                    s.IP,
                    s.Name,
                    s.DevNo,
                    s.SvonLogic
                    );
            });
        }

        #endregion

        public virtual void MovAbs(double dis, bool checkdone)
        {
            base.MovAbs(dis, checkdone, true);
        }

        public virtual void MovAbs(double dis, double speed, double acc, double dec, bool checkdone)
        {
            base.MovAbs(dis, speed, acc, dec, checkdone, true);
        }



        SaveReaddll.SaveReadHeler SaveRead;

        [ButtonRemark("回原点", EnumColor.LightGreen)]
        public override void Home()
        {
            base.Home();
        }
        public void Read()
        {
            SaveRead?.Read();
        }

        public void Save()
        {
            SaveRead.Save();
        }

        public virtual bool CheckSafePos()
        {
            return false;
        }


    }



    public static  class AixCreateBaseExtend
    {
        public static bool MovAbsCheck<T>(this T aix,Func<T,SingleAixPosAndSpeedMsgAndIMov> func) where T: AixCreateBase
        {
            var pos = func(aix);
            if(aix.CheckPos(pos.PosMsg.GetValue,0.05))
            {
                
                return true;
            }
            else
            {
                pos.Mov();
                if (aix.CheckPos(pos.PosMsg.GetValue, 0.05))
                {
                    return true;
                }
                 return false;
            }
        }
    }
}
